package com.piggy.logi.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.piggy.common.core.constant.SecurityConstants;
import com.piggy.common.core.utils.PageUtils;
import com.piggy.common.core.web.page.PagePlus;
import com.piggy.common.core.web.page.TableDataInfo;
import com.piggy.logi.sys.api.vo.LogiSysDeptExtVo;
import com.piggy.logi.sys.bo.*;
import com.piggy.logi.sys.domain.*;
import com.piggy.logi.sys.service.ILogiSysDeptService;
import com.piggy.logi.sys.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.piggy.logi.sys.mapper.LogiWaybillReceiptLogMapper;
import com.piggy.logi.sys.service.ILogiWaybillReceiptLogService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 运单回单跟踪信息Service业务层处理
 *
 * @author piggy
 * @date 2023-10-18
 */
@Slf4j
@Service
public class LogiWaybillReceiptLogServiceImpl extends ServiceImpl<LogiWaybillReceiptLogMapper, LogiWaybillReceiptLog> implements ILogiWaybillReceiptLogService {

    @Resource
    private ILogiSysDeptService logiSysDeptService;

    @Override
    public LogiWaybillReceiptLogVo queryById(Long id) {
        LogiWaybillReceiptLogVo vo = getVoById(id, LogiWaybillReceiptLogVo.class);
        if (ObjectUtil.isEmpty(vo)) {
            return vo;
        }
        return fillData(Lists.newArrayList(vo)).get(0);
    }

    @Override
    public TableDataInfo<LogiWaybillReceiptLogVo> queryPageList(LogiWaybillReceiptLogQueryBo bo) {
        PagePlus<LogiWaybillReceiptLog, LogiWaybillReceiptLogVo> result = pageVo(PageUtils.buildPagePlus(bo, true), buildQueryWrapper(bo), LogiWaybillReceiptLogVo.class);
        result.setRecordsVo(fillData(result.getRecordsVo()));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<LogiWaybillReceiptLogVo> queryList(LogiWaybillReceiptLogQueryBo bo) {
        return fillData(listVo(buildQueryWrapper(bo), LogiWaybillReceiptLogVo.class));
    }

    private LambdaQueryWrapper<LogiWaybillReceiptLog> buildQueryWrapper(LogiWaybillReceiptLogQueryBo bo) {
        LambdaQueryWrapper<LogiWaybillReceiptLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getWaybillId() != null, LogiWaybillReceiptLog::getWaybillId, bo.getWaybillId());
        lqw.eq(bo.getDeptId() != null, LogiWaybillReceiptLog::getDeptId, bo.getDeptId());
        return lqw;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LogiWaybillReceiptLogVo insertByAddBo(LogiWaybillReceiptLogEditBo bo) {
        LogiWaybillReceiptLog add = BeanUtil.toBean(bo, LogiWaybillReceiptLog.class);
        save(add);
        return queryById(add.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LogiWaybillReceiptLogVo updateByEditBo(LogiWaybillReceiptLogEditBo bo) {
        LogiWaybillReceiptLog update = BeanUtil.toBean(bo, LogiWaybillReceiptLog.class);
        updateById(update);
        return queryById(update.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    public List<LogiWaybillReceiptLogVo> fillData(List<LogiWaybillReceiptLogVo> logList) {
        if (CollUtil.isEmpty(logList)) {
            return logList;
        }
        List<Long> deptIds = logList.stream().map(LogiWaybillReceiptLogVo::getDeptId).distinct().collect(Collectors.toList());
        List<LogiSysDeptExtVo> sysDeptExtVos = logiSysDeptService.selectDeptByIds(deptIds);
        Map<Long, LogiSysDeptExtVo> sysDeptExtVoMap = sysDeptExtVos.stream().collect(Collectors.toMap(LogiSysDeptExtVo::getDeptId, Function.identity()));

        logList.forEach(Any -> {
            if (sysDeptExtVoMap.get(Any.getDeptId()) != null) {
                Any.setDeptName(sysDeptExtVoMap.get(Any.getDeptId()).getDeptName());
            }
        });

        return logList;
    }

}
